/**
 * Created by qianjy on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'pages', 'navigator'], function ($, pg) {
    var bankStatus = false;
    $('#all').bind('click', function () {
        $('#all').addClass('active');
        $('#week').removeClass('active');
        $('#month').removeClass('active');
        $('#season').removeClass('active');
    });
    $('#week').bind('click', function () {
        $('#all').removeClass('active');
        $('#week').addClass('active');
        $('#month').removeClass('active');
        $('#season').removeClass('active');
    });
    $('#month').bind('click', function () {
        $('#all').removeClass('active');
        $('#week').removeClass('active');
        $('#month').addClass('active');
        $('#season').removeClass('active');
    });
    $('#season').bind('click', function () {
        $('#all').removeClass('active');
        $('#week').removeClass('active');
        $('#month').removeClass('active');
        $('#season').addClass('active');
    });
    $('#popupclose').bind('click', function () {
        $('.rebox').hide();
        $('#validImg').click();
    });


    $.ajax({
        type: "POST",
        url: "/user/security/information",
        dataType: "json",
        success: function (data) {
            if (data.bank != null) {
                bankStatus = true;
                $('#banName').val(data.security.realname)
                $('#bankCardNum').val(data.security.accountNumber)
                $('.bankCard').attr('type', data.bank.bankCode);
                $('.bankCardName').text(data.bank.bankName);
                $('.bankCard-num').text(data.bank.cardNo);
            } else {
                bankStatus = false;
                $('#banName').val('')
                $('#bankCardNum').val('')
            }
            if (data.security.realnameStatus == 0) {
                layer.alert('未进行实名认证，即将跳转至实名认证页面');
                function jump() {
                    window.location.href = '/user/security/realname'
                }

                window.setTimeout(jump, 1500);
            }

        }
    });

    //点击获取验证码
    $('#validImg').click(function () {
        $("#validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });

    $('#rechargeAmount').keyup(function (){
        var rechargeAmount = $('#rechargeAmount').val();
        var reg = /^[0-9]*$/g;
        if (rechargeAmount.length > 11) {
            layer.tips('最多可输入11位', '#rechargeAmount')
            return;
        } else if (rechargeAmount > 99999999.99) {
            layer.tips('超过最大可充值金额', '#rechargeAmount')
            return;
        }
        else if (!reg.test(Number(rechargeAmount))) {
            layer.tips('请输入正确的充值金额', '#rechargeAmount')
            return;
        }
        else if (rechargeAmount < 0) {
            layer.tips('金额不能小于0', '#rechargeAmount')
            return;
        }
    })
    $('#rechargeAmount').blur(
        function () {
            var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/
            var rechargeAmount = $('#rechargeAmount').val();
            if (!exp.test(rechargeAmount)) {
                layer.tips('请输入正确的金额最多保留两位小数', '#rechargeAmount')
                return;
            }
        }
    )

    var url = '/account/recharge/pictureCodeCheck';

    $('.choicechongzhi li').bind('click', function () {
        if ($(this).attr('id') == '1') {
            $('.wangyinchongzhi').addClass('liactive');
            $('.kuaijiechongzhi').removeClass('liactive');
            $('.kuaijiechongzhidiv').hide();
            $('#rechargeBut').show();
            $('#rechargeBut1').hide();
            $('.third-pay1').show();
            $('.add').hide();
            $("#rechargeAmount").val("");
        } else if ($(this).attr('id') == '2') {
            if (!bankStatus) {
                layer.alert('未绑卡，即将跳转至绑卡页面');
                function jump() {
                    window.location.href = '/user/security/realname'
                }
                window.setTimeout(jump, 1500);
                return;
            }
            getMoney();
            $('.kuaijiechongzhidiv').show();
            $('.kuaijiechongzhi').addClass('liactive');
            $('.wangyinchongzhi').removeClass('liactive')
            $('#rechargeBut').hide();
            $('#rechargeBut1').show();
            $('.third-pay1').hide();
            $('.add').show()
            $("#rechargeAmount,#pictureCode").val("");
        }
    })



    //快捷充值
    $("#rechargeBut1").on("click", function (){
        var rechargeAmount = $('#rechargeAmount').val();
        var pictureCode = $('#pictureCode').val();
        if (!rechargeAmount) {
            layer.tips('请输入充值金额', '#rechargeAmount');
            return;
        }
        $(".rebox1").css({"display": "block"});
        $("#payPassword_rsainput").val("").focus();
        $(".guangbiao").css({"left": 0});
        $(".sixDigitPassword").find("i").find("b").css({"display": "none"});
    })
    $("#cancel1").bind("click", function () {
        $(".rebox1").css({"display": "none"});
        $('#payPassword_rsainput').val('');
        $(".guangbiao").css({"left": 0});
        $(".sixDigitPassword").find("i").find("b").css({"display": "none"});
    })
    //支付弹框
    $("#payPassword_rsainput").bind("blur", function () {
        $(".sixDigitPassword span").removeClass("guangbiao");
    });
    $("#payPassword_rsainput").bind("focus", function () {
        $(".sixDigitPassword span").addClass("guangbiao");
        if ($(".i-text").val().length == 6) {

        } else {
            $(".guangbiao").css({"left": ($(".i-text").val().length) * 50});
        }
    });
    $(".i-text").keyup(function () {
        var inp_v = $(this).val();
        var inp_l = inp_v.length;
        //$("p").html( "input的值为：" + inp_v +"; " + "值的长度为:" + inp_l);//测试用
        for (var x = 0; x <= 6; x++) {
            //$("p").html( inp_l );//测试
            $(".sixDigitPassword").find("i").eq(inp_l).prevAll("i").find("b").css({"display": "block"});
            $(".sixDigitPassword").find("i").eq(inp_l - 1).nextAll("i").find("b").css({"display": "none"});
            $(".guangbiao").css({"left": inp_l * 50});//光标位置
            if (inp_l == 0) {
                $(".sixDigitPassword").find("b").css({"display": "none"});
                $(".guangbiao").css({"left": 0});
            }
            else if (inp_l == 6) {
                $(".sixDigitPassword").find("b").css({"display": "block"});
                $(".guangbiao").css({"left": 5 * 50});
            }
        }
    });
    //点击确定
    $('#determine1').bind('click', function () {
        var password = $('#payPassword_rsainput').val();
        var rechargeAmount = $('#rechargeAmount').val();
        var pictureCode = $('#pictureCode').val();
        var loading = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        if (password != '') {
            $.ajax({
                type: 'post',
                url: '/account/recharge/applyRecharge ',
                data: {
                    payPassword: password,
                    amount: rechargeAmount
                },
                dataType: 'json',
                success: function (data) {

                    if (data.result == 'fail') {
                        layer.close(loading)
                        layer.msg(data.messageText)
                        layer.close(loading)
                    } else {
                        layer.msg('申请成功')
                        function jump() {
                            window.location.href = '/user/assets'
                        }
                        window.setTimeout(jump, 1500);
                    }
                }
            })
        }else{
            $('#payPassword_rsainput').val('');
            layer.msg('请输入交易密码');
            layer.close(loading);
        }
    });
    //支付页回车事件
    $(".rebox1").keydown(function () {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#determine').click();
        }
    });

    //三位数字加一个逗号并且保留两位小数
    function threeNumber(num) {
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
                num.substring(num.length - (4 * i + 3));
        return (((sign) ? '' : '-') + num + '.' + cents);
    }

    //查询银行卡充值金额
    function getMoney() {
        $.ajax({
            type: 'post',
            url: '/account/recharge/rechargeLimit',
            dataTape: 'json',
            success: function (data) {
                if (data.limitAmount.singleLimitAmount == '99999999') {
                    $('.panMoney').html('无限')
                } else {
                    $('.panMoney').html(threeNumber(data.limitAmount.singleLimitAmount));
                }
                if (data.limitAmount.dayLimitAmount == '99999999') {
                    $('.dayMoney').html('无限');
                } else {
                    $('.dayMoney').html(threeNumber(data.limitAmount.dayLimitAmount));
                }
                if (data.limitAmount.monthLimitAmount == '99999999') {
                    $('.monthMoney').html('无限');

                } else {
                    $('.monthMoney').html(threeNumber(data.limitAmount.monthLimitAmount));
                }
            }
        })
    }

    //网银充值
    $('#rechargeBut').bind('click', function (){
        var rechargeAmount = $('#rechargeAmount').val();
        var pictureCode = $('#pictureCode').val();
        if (!rechargeAmount) {
            layer.tips('请输入充值金额', '#rechargeAmount');
            return;
        }
        else if (!pictureCode) {
            layer.tips('请输入验证码', '#pictureCode');
            return;
        } else if (pictureCode.length != 4) {
            layer.tips('验证码错误', '#pictureCode');
            return;
        }
        var loading = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
        $.ajax({
            type: 'post',
            url: url,
            data: {
                pictureCode: pictureCode
            },
            dataType: 'json',
            async: false,
            success: function (data) {
                layer.close(loading);
                $('#validImg').trigger('click');
                if (data.result == 'fail') {
                    layer.msg(data.respMsg)
                } else if (data.result == 'success') {
                    var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/
                    var rechargeAmount = $('#rechargeAmount').val();
                    var host = window.location.host;
                    if (exp.test(rechargeAmount)) {
                        window.open('http://' + host + '/account/recharge/platformRecharge?rechargeAmount=' +
                            rechargeAmount);
                        $('.rebox').show();

                    } else {
                        layer.msg('请输入正确的金额')
                    }
                }
            }
        })
    })


    //限额表
    $.ajax({
        type: "POST",
        url: "/account/limitAmount/getList ",
        data: {pageSize: 20,pageNum:1},
        dataType: 'json',
        async: true,
        success: function (data) {
            console.log(data);
            var textList = "";
            var titleText = "<ul class='title'><li>银行</li><li>单笔限额(元)</li><li>日限额(元)</li><span class='close'>X</span></ul>"
            for (var i = 0; i < data.list.length; i++) {
                textList += "<ul class='text'><li>" + data.list[i].bankName + "</li><li>" + data.list[i].singleLimitAmount + "</li><li>" + data.list[i].dayLimitAmount + "</li></ul>"
            }
            $(".bankBox").html(titleText + "<div style='max-height: 360px;overflow: auto;'>"+textList +"</div>");
            $('.close').on('click', function () {
                $('.bankAlertBox').hide();
            })
        }
    });


    $('.lookXianEdu').bind('click', function (){
        $('.bankAlertBox').show();
    })

});

